﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1
{
	public partial class Game1
	{
		/// <summary>
		/// Allows the game to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Update(GameTime gameTime)
		{
			// Allows the game to exit
			if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
				this.Exit();

			// TODO: Add your update logic here

			lastKeyboardState = currentKeyboardState;
			currentKeyboardState = Keyboard.GetState();

			// Allows the game to exit
			if (currentKeyboardState.IsKeyDown(Keys.Escape))
				Exit();
			/*
			foreach (var key in currentKeyboardState.GetPressedKeys())
			{
				switch (key)
				{
					case Keys.Left:
						gameCamera.RotateHorizontally(-0.05f);
						break;
					case Keys.Right:
						gameCamera.RotateHorizontally(0.05f);
						break;
					case Keys.Up:
						gameCamera.RotateVertically(0.05f);
						break;
					case Keys.Down:
						gameCamera.RotateVertically(-0.05f);
						break;
					case Keys.A:
						gameCamera.MoveUpDown(-0.5f);
						break;
					case Keys.D:
						gameCamera.MoveUpDown(0.5f);
						break;
					case Keys.W:
						gameCamera.MoveFront(0.5f);
						break;
					case Keys.S:
						gameCamera.MoveFront(-0.5f);
						break;
					case Keys.Q:
						gameCamera.Strafe(0.5f);
						break;
					case Keys.E:
						gameCamera.Strafe(-0.5f);
						break;
					default:
						break;
				}
			}//*/

			MouseState currentMouseState = Mouse.GetState();
			if (currentMouseState != originalMouseState)
			{
				float xDifference = currentMouseState.X - originalMouseState.X;
				float yDifference = currentMouseState.Y - originalMouseState.Y;
				Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);

				gameCamera.AddMouseMovement(xDifference, yDifference, (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);
			}

			Vector3 moveVector = new Vector3(0, 0, 0);
			KeyboardState keyState = currentKeyboardState;
			if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
				moveVector += new Vector3(0, 0, -1);
			if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
				moveVector += new Vector3(0, 0, 1);
			if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
				moveVector += new Vector3(1, 0, 0);
			if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
				moveVector += new Vector3(-1, 0, 0);
			if (keyState.IsKeyDown(Keys.Q))
				moveVector += new Vector3(0, 1, 0);
			if (keyState.IsKeyDown(Keys.Z))
				moveVector += new Vector3(0, -1, 0);

			gameCamera.AddToCameraPosition(moveVector, (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);

			base.Update(gameTime);
		}
	}
}